package com.ruoyi.project.system.state.controller;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.trick.domain.VerbalTrick;
import com.ruoyi.project.system.trick.service.IVerbalTrickService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.state.domain.DataState;
import com.ruoyi.project.system.state.service.IDataStateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 数据状态Controller
 * 
 * @author ruoyi
 * @date 2022-02-20
 */
@Controller
@RequestMapping("/system/state")
public class DataStateController extends BaseController
{
    private String prefix = "system/state";

    @Autowired
    private IDataStateService dataStateService;
    @Autowired
    private IVerbalTrickService verbalTrickService;

//    @RequiresPermissions("system:state:view")
    @GetMapping()
    public String state()
    {
        return prefix + "/state";
    }

    /**
     * 查询数据状态列表
     */
//    @RequiresPermissions("system:state:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataState dataState)
    {
        startPage();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if (!loginUser.equals("admin")){
            dataState.setUserName(loginUser);
        }
        List<DataState> list = dataStateService.selectDataStateList(dataState);
        return getDataTable(list);
    }

    /**
     * 导出数据状态列表
     */
//    @RequiresPermissions("system:state:export")
    @Log(title = "数据状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DataState dataState)
    {
        List<DataState> list = dataStateService.selectDataStateList(dataState);
        ExcelUtil<DataState> util = new ExcelUtil<DataState>(DataState.class);
        return util.exportExcel(list, "数据状态数据");
    }

    /**
     * 新增数据状态
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增保存数据状态
     */
//    @RequiresPermissions("system:state:add")
    @Log(title = "数据状态", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DataState dataState)
    {
        String userName = dataState.getUserName();
        Long equipmentId = dataState.getEquipmentId();
        String clientName = dataState.getClientName();
        String wechatId1 = dataState.getWechatId();
        String throughState = dataState.getThroughState();
        if (userName.isEmpty()){
//           return AjaxResult.error("用户名不能为空","用户名不能为空");
            return AjaxResult.warn("登录名称不能为空");
        }
        if (equipmentId==null){
            return AjaxResult.warn("设备名称不能为空");
        }
        if (clientName.isEmpty()){
            return AjaxResult.warn("客户名称不能为空");
        }
//        if (wechatId1.isEmpty()){
//            return AjaxResult.warn("微信名称不能为空");
//        }
        if (throughState.isEmpty()){
            return AjaxResult.warn("通过状态不能为空");
        }

//        String throughState = dataState.getThroughState();
        if (throughState.equals("1")){
            dataState.setThroughState("已通过");
        }else {
            dataState.setThroughState("未通过");
        }
    //    String userName = dataState.getUserName();
        // 获取当前的用户信息
        //User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        //String loginUser = currentUser.getLoginName();

        //获取设备id
  //      Long equipmentId = dataState.getEquipmentId();
        //根据设备id和用户名查询微信号
        VerbalTrick verbalTrick = verbalTrickService.selectVerbalTrickByEquipmentIdAndUserName(equipmentId, userName);
        String wechatId = verbalTrick.getWechatId();
        dataState.setWechatId(wechatId);
        return toAjax(dataStateService.insertDataState(dataState));
    }



    /**
     * 修改数据状态
     */
//    @RequiresPermissions("system:state:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DataState dataState = dataStateService.selectDataStateById(id);
        mmap.put("dataState", dataState);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据状态
     */
//    @RequiresPermissions("system:state:edit")
    @Log(title = "数据状态", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DataState dataState)
    {
        return toAjax(dataStateService.updateDataState(dataState));
    }

    /**
     * 删除数据状态
     */
//    @RequiresPermissions("system:state:remove")
    @Log(title = "数据状态", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dataStateService.deleteDataStateByIds(ids));
    }
}
